{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "981d8b7b",
   "metadata": {},
   "source": [
    "-----------\n",
    "* 日期：2022-11-2 \\week 11 元组_集合_字典\n",
    "* 课程：python\n",
    "* 记录人：洪梦瑶\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06e003ba",
   "metadata": {},
   "source": [
    "# 1. 元组（tuple）\n",
    "> 1. 元组的概念和基本形态\n",
    "\n",
    "> 2. 元组的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "7b9b2841",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(18001, 18002, 18003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "user_info = 18001,18002,18003\n",
    "print(user_info,type(user_info)) # 创造元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "3aaccfd7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[18001, 18002, 18003] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "user_info_list = [18001,18002,18003]\n",
    "print(user_info_list,type(user_info_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "643ae389",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18001\n",
      "(18001, 18002)\n"
     ]
    }
   ],
   "source": [
    "# 取值\n",
    "print(user_info[0])\n",
    "# 切片\n",
    "print(user_info[:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "daf0f71e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 举例\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "8be47bbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=11, tm_mday=2, tm_hour=20, tm_min=19, tm_sec=13, tm_wday=2, tm_yday=306, tm_isdst=0)"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "9fd2fb02",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 占空间，注释掉，有需要可去掉“#”\n",
    "# help(time.localtime())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "db78c4df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1667391554.4509153"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.time() # 时间戳，可以记录互联网当中所有 数据信息产生、销毁、创建一系列行为"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "cf295a96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.count(18001) # count 查看数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "5b98db9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.index(18001) # index 检查索引值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9406c806",
   "metadata": {},
   "source": [
    "# 2. 集合（Sets）\n",
    "\n",
    "> 1. 集合的概念和性质\n",
    ">> 1. 集合的唯一性\n",
    ">> 2. 集合的无序性\n",
    "> 2. 集合的方法\n",
    ">> 1. 交集：    逻辑运算符：     setA & setB   ,setA.intersection(B)   =>  应用：推荐系统\n",
    ">> 2. 并集：    逻辑运算符：     setA | setB   ,setA.union(B)        =>  应用：快速统计两个集合元素的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "b528f66f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'orange', 'pear', 'banana'} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "print(basket,type(basket))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "2fa419fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "351a6144",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {1801,1802,1803,1809,1811,1812}\n",
    "合唱团 = {1808,1822,1802,1801,1888,1899}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "1b81e531",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802}"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 & 合唱团"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "c6705d9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802}"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.intersection(合唱团)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "0a85a566",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "e6ed4b07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1808, 1809, 1811, 1812, 1822, 1888, 1899}"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 | 合唱团 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "05308bd2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社 | 合唱团) # 集合元素的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "fb32854a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1803, 1808, 1809, 1811, 1812, 1822, 1888, 1899}"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.union(合唱团)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "2429894d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考： 如果是列表的话,想要找出重复的项？集合的价值\n",
    "舞蹈社 = {1801,1802,1803,1809,1811,1812}\n",
    "合唱团 = {1808,1822,1802,1801,1888,1899}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a712156b",
   "metadata": {},
   "source": [
    "# 3. 字典（dict）\n",
    "> 1. 字典的概念和性质\n",
    ">> 1. 性质：\n",
    ">>> 1. 无序性\n",
    ">>> 2. key 是唯一的(没有重复)\n",
    ">>> 3. key 是不可变的\n",
    "\n",
    "> 2. 字典的创建\n",
    "\n",
    "```\n",
    "(1) 按照字典的形式直接创建(key:value)\n",
    "网新1班 = {\n",
    "    1800001:\"梨花\",\n",
    "    1800002:\"小明\",\n",
    "    1800003:\"Marry\",\n",
    "    1800004:\"Jack\"\n",
    "}\n",
    "\n",
    "(2) 使用dict()方法创建字典\n",
    "网新2班 = dict(\n",
    "    stu1900001=\"小张\",\n",
    "    stu1900002=\"小林\",\n",
    "    stu1900003=\"小陈\"\n",
    ")\n",
    "```\n",
    "\n",
    "> 3. 字典的方法\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "3fa400f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "wechat = {\n",
    "    \"fgyqeu7489fhdjk\":[\"是小问问呀\",\"mysluck1111\",\"朋友圈\",\"置顶聊天\",\"中国山头\"],\n",
    "    \"hfuefukekdfj233ii\":[\"Jerry\",\"美国\"]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "df160fc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['是小问问呀', 'mysluck1111', '朋友圈', '置顶聊天', '中国山头']"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wechat[\"fgyqeu7489fhdjk\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "1b983ee1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: '梨花', 1800002: '小明', 1800003: 'Marry', 1800004: 'Jack'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "网新1班 = {\n",
    "    1800001:\"梨花\",\n",
    "    1800002:\"小明\",\n",
    "    1800003:\"Marry\",\n",
    "    1800004:\"Jack\"\n",
    "}\n",
    "print(网新1班,type(网新1班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "d1daff8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'梨花'"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新1班[1800001]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "d0932add",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1900001': '小张', 'stu1900002': '小林', 'stu1900003': '小陈'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "网新2班 = dict(\n",
    "    stu1900001=\"小张\",\n",
    "    stu1900002=\"小林\",\n",
    "    stu1900003=\"小陈\"\n",
    ")\n",
    "print(网新2班,type(网新2班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "512f11df",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 课后：请大家预习字典的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06b83ebd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
